package cn.wolfcode.luowowo.comment.service.impl;

import cn.wolfcode.luowowo.acticle.domain.ScenicSpot;
import cn.wolfcode.luowowo.acticle.query.TravelQuery;
import cn.wolfcode.luowowo.acticle.service.IScenicSpotService;
import cn.wolfcode.luowowo.comment.domain.ScenicSpotComment;
import cn.wolfcode.luowowo.comment.query.ScenicSpotCommentQuery;
import cn.wolfcode.luowowo.comment.repository.IScenicSpotCommentRepository;
import cn.wolfcode.luowowo.comment.service.IScenicSpotCommentService;
import cn.wolfcode.luowowo.common.util.BeanUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
public class ScenicSpotCommentServiceImpl implements IScenicSpotCommentService{
    @Autowired
    private IScenicSpotCommentRepository scenicSpotCommentRepository;
    @Reference
    private IScenicSpotService scenicSpotService;
    @Autowired
    private MongoTemplate template;

    @Override
    public ScenicSpotComment saveOrUpdate(ScenicSpotComment comment) {
        if(!StringUtils.hasLength(comment.getId())){
            comment.setCreateTime(new Date());
        }
        if (comment.getType()==1){
        if(StringUtils.hasLength(comment.getRefComment().getId())){
            ScenicSpotComment refComment = scenicSpotCommentRepository.findById(comment.getRefComment().getId()).get();
            comment.setRefComment(refComment);
        }
        }
        scenicSpotCommentRepository.save(comment);
        return comment;
    }

    @Override
    public Page query(ScenicSpotCommentQuery qo) {
        //查询条件
        Query query = new Query();
        //判断
        if (qo.getScenicId()!=-1){
            query.addCriteria(Criteria.where("scenicId").is(qo.getScenicId()));
            query.addCriteria(Criteria.where("refComment").is(null));
            if (qo.getRank()!=null && qo.getRank()!=-1 ){
                query.addCriteria(Criteria.where("rank").is(qo.getRank()));
            }
        }
        //查询总数
        long count = template.count(query, ScenicSpotComment.class);

        //判断是否有符合条件的评论
        if(count == 0){
            return Page.empty();
        }

        //分页条件
        Pageable pageable = PageRequest.of(qo.getCurrentPage() -1 ,qo.getPageSize(),new Sort(Sort.Direction.DESC,"createTime"));
        query.with(pageable);

        List<ScenicSpotComment> scenicSpotComments = template.find(query, ScenicSpotComment.class);
        return new PageImpl(scenicSpotComments,pageable,count);
    }

    @Override
    public Page queryAll(ScenicSpotCommentQuery qo) {
        //查询条件
        Query query = new Query();

        //判断
        if (qo.getScenicId()==-1){
            return Page.empty();
        }
        Criteria criteria=(new Criteria().andOperator(
                Criteria.where("scenicId").is(qo.getScenicId()),
                Criteria.where("type").is(1)
        ));
        query.addCriteria(criteria);
        //查询总数
        long count = template.count(query, ScenicSpotComment.class);

        //判断是否有符合条件的评论
        if(count == 0){
            return Page.empty();
        }

        //分页条件
        Pageable pageable = PageRequest.of(qo.getCurrentPage() -1 ,qo.getPageSize(),new Sort(Sort.Direction.ASC,"createTime"));
        query.with(pageable);

        List<ScenicSpotComment> scenicSpotComments = template.find(query, ScenicSpotComment.class);
        return new PageImpl(scenicSpotComments,pageable,count);
    }

    @Override
    public Long getCount(ScenicSpotCommentQuery qo) {
        //查询条件
        Query query = new Query();
        //判断
        if (qo.getScenicId()!=-1){
            query.addCriteria(Criteria.where("refComment").is(null));
            query.addCriteria(Criteria.where("scenicId").is(qo.getScenicId()));
            if (qo.getRank()!=null && qo.getRank()!=-1 ){
                query.addCriteria(Criteria.where("rank").is(qo.getRank()));
            }
        }
        //查询总数
        long count = template.count(query, ScenicSpotComment.class);

        return count;
    }

    public Long getCount1(long scenicId, int num) {
        Query query = new Query();
        query.addCriteria(Criteria.where("scenicId").is(scenicId));
        query.addCriteria(Criteria.where("rank").is(num));
        Criteria.where("refComment").is(null);

        //查询总数
        long count = template.count(query, ScenicSpotComment.class);

        return count;
    }

    @Override
    public  List<ScenicSpotComment> selectByUserId(Long userId) {
        //创建查询对象
        Query query = new Query();
        query.addCriteria(Criteria.where("userId").is(userId));
        List<ScenicSpotComment> list = template.find(query, ScenicSpotComment.class);
        List<ScenicSpotComment> ssclist = new ArrayList<>();
        for (ScenicSpotComment scenicSpotComment : list) {
            ScenicSpotComment ssc = new ScenicSpotComment();
            ScenicSpot scenicSpot =scenicSpotService.getDestByScenicId(scenicSpotComment.getScenicId());
            BeanUtil.copyProperties(ssc,scenicSpotComment);
            ssc.setScenicSpot(scenicSpot);
            ssclist.add(ssc);
            System.out.println(ssclist);
        }
        return ssclist;
    }

}
